package com.shine.icis.action;


import java.net.URLEncoder;
import java.util.Collection;
import java.util.UUID;

import com.shine.cotrun.sql.QueryParameters;
import com.shine.cotrun.web.comm.ListPage;
import com.shine.cotrun.web.upload.FileDispatcher;
import com.shine.icis.blo.IDataSubmitService;
import com.shine.icis.blo.IOfConfirmProcService;
import com.shine.icis.config.SysCodeDictLoader;
import com.shine.icis.config.SysConstants;
import com.shine.icis.dto.StatAmounts;
import com.shine.icis.hbean.OfConfirmProc;
import com.shine.icis.hbean.SubmitConf;

public class OfConfirmProcAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8949895938587598815L;
	private IOfConfirmProcService ofConfirmProcService;
	private IDataSubmitService dataSubmitService;
	
	//input param
	private int pageNo;
	private int pageSize;
	private String busiDate;//业务日期
	private String expType;//导出方式
	
	private int id;
	private String orderDate;// 交易日期
	private String confirmDate;//确认日期
	private String settleDate;//交收日期
	private String arapType;// 应收应付类型
	private String oppPar;// 对方机构
	private String mktCode;// 市场代码
	private String arapBusiType;// 业务类型
	private String curType;// 币种
	private double arapAmt;// 金额

	private String currentBusiDate;//当前业务日期
	private StatAmounts statAmounts;//统计金额
	private Collection<SubmitConf> curTypeSipfConfs;//A13币种
	private Collection<SubmitConf> oppParConfs;//A13对方机构
	private Collection<SubmitConf> mktCodeSipfConfs;//A13市场代码
	private Collection<SubmitConf> arapBusiTypeConfs;//A13业务类型
	private ListPage<OfConfirmProc> ofConfirmProcPage;
	private String downloadUrl;//下载地址
	
	private OfConfirmProc ofConfirmProc;
	
	
	public void setOfConfirmProcService(IOfConfirmProcService ofConfirmProcService) {
		this.ofConfirmProcService = ofConfirmProcService;
	}
	public void setDataSubmitService(IDataSubmitService dataSubmitService) {
		this.dataSubmitService = dataSubmitService;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public void setBusiDate(String busiDate) {
		this.busiDate = busiDate;
	}
	public void setExpType(String expType) {
		this.expType = expType;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	public void setOrderDate(String orderDate) {
		this.orderDate = orderDate;
	}
	public void setConfirmDate(String confirmDate) {
		this.confirmDate = confirmDate;
	}
	public void setSettleDate(String settleDate) {
		this.settleDate = settleDate;
	}
	public void setOppPar(String oppPar) {
		this.oppPar = oppPar;
	}
	public void setMktCode(String mktCode) {
		this.mktCode = mktCode;
	}
	public void setArapBusiType(String arapBusiType) {
		this.arapBusiType = arapBusiType;
	}
	public void setCurType(String curType) {
		this.curType = curType;
	}
	public void setArapAmt(double arapAmt) {
		this.arapAmt = arapAmt;
	}
	public void setArapType(String arapType) {
		this.arapType = arapType;
	}
	public String getCurrentBusiDate() {
		return currentBusiDate;
	}
	public StatAmounts getStatAmounts() {
		return statAmounts;
	}
	public Collection<SubmitConf> getCurTypeSipfConfs() {
		return curTypeSipfConfs;
	}
	public Collection<SubmitConf> getOppParConfs() {
		return oppParConfs;
	}
	public Collection<SubmitConf> getMktCodeSipfConfs() {
		return mktCodeSipfConfs;
	}
	public Collection<SubmitConf> getArapBusiTypeConfs() {
		return arapBusiTypeConfs;
	}
	public ListPage<OfConfirmProc> getOfConfirmProcPage() {
		return ofConfirmProcPage;
	}
	public String getDownloadUrl() {
		return downloadUrl;
	}

	public OfConfirmProc getOfConfirmProc() {
		return ofConfirmProc;
	}
	
	/**
	 * 转到查询页面
	 * @return
	 */
	public String initQueryOfConfirmProc() {
		currentBusiDate = dataSubmitService.getBusiDate();
		statAmounts = ofConfirmProcService.getStatAmount();
		
		curTypeSipfConfs = SysCodeDictLoader.getInstance().getCurTypeSipfConfs();
		oppParConfs = SysCodeDictLoader.getInstance().getOppParConfs();
		mktCodeSipfConfs = SysCodeDictLoader.getInstance().getMktCodeSipfConfs();
		arapBusiTypeConfs = SysCodeDictLoader.getInstance().getArapBusiTypeConfs();
		return success();
	}
	
	/**
	 * 翻页查询
	 * @return
	 */
	public String queryOfConfirmProc() {
		if (busiDate == null || busiDate.trim().equals("")) {
			return error("参数不能为空");
		}
		QueryParameters pqp = new QueryParameters();
		
		//设置分页信息
		pqp.setPageNo(pageNo);
		pqp.setPageSize(pageSize);
		//增加对排序列的处理
		pqp.addOrder("arapType", true);
		pqp.addOrder("oppPar", true);
		pqp.addOrder("arapBusiType", true);
		
		//设置查询参数
		pqp.addParameter("busiDate", Integer.parseInt(busiDate.trim().replaceAll("-", "")));
		
		try {
			ofConfirmProcPage = ofConfirmProcService.getOfConfirmProcPage(pqp);
			return success();
		} catch (Exception e) {
			e.printStackTrace();
			return error();
		}
	}
	
	/**
	 * 导出Excel
	 */
	public String exportOfConfirmProc() {
		if (busiDate == null || busiDate.trim().equals("")) {
			return error("参数不能为空");
		}
		QueryParameters pqp = new QueryParameters();
		
		//增加对排序列的处理
		pqp.addOrder("arapType", true);
		pqp.addOrder("oppPar", true);
		pqp.addOrder("arapBusiType", true);
		
		//设置查询参数
		pqp.addParameter("busiDate", Integer.parseInt(busiDate.trim().replaceAll("-", "")));
		try {
			//生成一个随机文件夹保证文件不重复
			String randomDir = UUID.randomUUID().toString() + "/"; 
			String saveDir = FileDispatcher.getTempDir() + randomDir;//文件保存绝对路径
			String absDir = FileDispatcher.getTempAbsDir() + randomDir;//WEB访问的相对路径
			String temFileName = ofConfirmProcService.csExpOfConfirmProc(pqp, saveDir, expType);
			downloadUrl = absDir + URLEncoder.encode(temFileName, SysConstants.DOWNLOAD_FILENAME_ENCODING);
		} catch(Exception e) {
			e.printStackTrace();
		}
		return success();
	}

	/**
	 * 删除
	 * @return
	 */
	public String deleteOfConfirmProc() {
		try {
			ofConfirmProcService.deleteOfConfirmProc(id);
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		}
	}
	
	/**
	 * 新增
	 * @return
	 */
	public String addOfConfirmProc() {
		if (settleDate == null || settleDate.trim().equals("") || 
				arapType == null || arapType.trim().equals("") || 
				oppPar == null || oppPar.trim().equals("") ||
				arapBusiType == null || arapBusiType.trim().equals("") ||
				curType == null || curType.trim().equals("")) {
			return error("参数不能为空");
		}
		try {
			if (orderDate != null) {
				orderDate = orderDate.trim().replaceAll("-", "");
			}
			if (confirmDate != null) {
				confirmDate = confirmDate.trim().replaceAll("-", "");
			}
			settleDate = settleDate.trim().replaceAll("-", "");
			ofConfirmProcService.addOfConfirmProc(Integer.parseInt(orderDate), Integer.parseInt(confirmDate), Integer.parseInt(settleDate), arapType, oppPar, mktCode, arapBusiType, curType, arapAmt);
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		}
	}
	
	/**
	 * 修改
	 * @return
	 */
	public String modifyOfConfirmProc() {
		if (arapType == null || arapType.trim().equals("")) {
			return error("参数不能为空");
		}
		try {
			ofConfirmProcService.modifyOfConfirmProc(id, arapType);
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		}
	}
	
	/**
	 * 重载数据
	 * @return
	 */
	public String loadOfConfirmProcData(){
		try {
			ofConfirmProcService.txGenOfConfirmProcData(true);
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		}
	}
	
	/**
	 * 确认生成数据
	 * @return
	 */
	public String cfmOfConfirmProcData(){
		try {
			ofConfirmProcService.txGenOfConfirmProcData(false);
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		}
	}
	
	/**
	 * 查看详情
	 * @return
	 */
	public String viewOfConfirmProcDetail() {
		try {
			ofConfirmProc = ofConfirmProcService.getOfConfirmProcById(id);
		return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		}
	}
}
